import { getRequestId } from '@/utils/tools'
export default {
    uploadFiles(files) {

        return new Promise((reslove, reject) => {
            let reqList = []

            files.forEach(f => {
                let fileExt = f.type.split('/')[1]
                let tempFileName = `post/postImg_${new Date().getTime()}_${getRequestId()}`
                reqList.push({
                    cloudPath: `${tempFileName}.${fileExt}`,
                    filePath: f,
                })
            })

            Promise.all(
                reqList.map((req) => {
                    return new Promise((reslove, reject) => {
                        this.uploadFile(req).then(res => {
                            console.log(`图片上传成功`, req, res);
                            reslove(res)
                        }).catch(err => {
                            reject(err)
                        })
                    })
                })
            ).then((resList) => {
                console.log('所有图片上传完成', resList);
                let imgList = resList.map(res => {
                    return res.fileID
                })
                reslove(imgList)
            }).catch((errs) => {
                reject(errs)
            })

        })


    },

    getFilesUrl(fileList) {
        return new Promise((resolve, reject) => {
            this.getTempFileURL({
                fileList: fileList
            }).then((res) => {
                console.log('获取文件临时链接url', res.fileList);
                let urlList = res.fileList.map(f => {
                    return {
                        fileID: f.fileID,
                        url: f.tempFileURL
                    }
                })
                resolve(urlList)
            }).catch(err => {
                reject(err)
            })
        })

    }

};
